package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.changgou.pojo.Brand;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-08-01
 * @Version V1.0
 **/
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("findAll")
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询所有执行成功", brandService.findAll());
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable int id) {
        return new Result(true, StatusCode.OK, "根据id查询成功", brandService.findById(id));
    }

    @PostMapping("add")
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result("添加品牌成功", null);
    }

    @PutMapping("update/{id}")
    public Result update(@PathVariable int id, @RequestBody Brand brand) {
        brand.setId(id);
        brandService.update(brand);
        return new Result("更新成功", null);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable int id) {
        brandService.delete(id);
        return new Result("删除成功", null);
    }

    @GetMapping("search/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                             @RequestParam Map<String, Object> searchMap) {
        Page<Brand> page = brandService.search(pageNum, pageSize, searchMap);
        long total = page.getTotal();
        List<Brand> result = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setRows(result);
        pageResult.setTotal(total);
        return new Result("分页查询成功", pageResult);

    }
}
